var packageJarNameFtp = "";
var updatePackageJarNameFtp = "";
var orginPackageJarNameFtp = "";
//var isBundleName="";
var bundleJarIdsFtp = "";
$(document).ready(function() {
	
	$('#import_linkFtp').window({
		width:465,
		modal:true,
		minimizable:false,
		maximizable:false,
		collapsible:false
	});
	$('#import_linkFtp').window('close');  //关闭窗口
	
	
	
	$("#fileFtp").uploadify({
		       
            "uploader": BASE_URL_HTTP_INTERFACE + '/uploadFile',//url
           	//指定swf文件
            "swf":"../../../js/uploadify/uploadify.swf?id=<%=System.currentTimeMillis()%>",
            "cancelImg": "../../../js/uploadify/uploadify-cancel.png",
            "queueID": "contentFtp",//上传文件队列div的id
            "auto": false,//是否自动上传
            "buttonText": "浏览",
            "fileObjName ": "filedata",//传到服务器的参数名
            "width":"110px",
            "method": "POST",
            "fileTypeDesc": '请选择要上传的。jar文件！',
            "fileTypeExts": '*.jar',
            "onSelect":function(file){
            	$("#contentFtp").css("display","block");
            	//$("#uploadJar").height($("#content").height() +　150);
            },
            "onUploadStart":function(file){
            	$("#uploadTableFtp").css("display","block");
            	//$("#uploadJar").height($("#uploadTable").height() +　150);
            },
            "onUploadSuccess" : function(file,data,response) {//上传完成时触发（每个文件触发一次）
            	//alert("onUploadSuccess");
            	//$("#uploadJar").height($("#fileUpload").height() +　20);
				var obj=eval("("+data+")");
				var flag=obj.success;
				//$("#fileUrl").val($("#fileUrl").val()+obj.fileUrl+","); 
				if(flag){
					$("#uploadTableFtp tbody").append("<tr name='"+obj.fileName+"'>" +
												"<td>"+obj.fileName+"</td>"+
												"<td>上传成功</td>"+
												"<td><a href='javascript:void(0)' "+
												"onclick='deleteFile(\""+obj.fileName+"\")'>删除文件</a></td>" +
												"</tr>");
					//alert($("#packageJarName").val() + obj.fileName);
					packageJarNameFtp += (packageJarNameFtp != ""? ",":"") + obj.fileName;
					/*if(obj.fileName != "" || obj.fileName != null){
						packageName.push(obj.fileName);
					}*/
				}else{
					$("#uploadTableFtp tbody").append("<tr name='"+obj.fileName+"'><td>"+obj.fileName+"</td>"+
												"<td>上传失败</td>"+
												"<td><a href='javascript:void(0)' "+
												"onclick='deleteFile(\""+obj.fileName+"\")'>删除文件</a></td></tr>");
					alert(obj.msg);
				}
				
              	$.parser.parse($("#uploadTableFtp"));
            }

        });
	
});		


function import_linkFtp(){
	$('#import_linkFtp').window('open'); 
}

function closeImport_linkFtp(){
	bundleJarIdsFtp = "";
	$("input[name='is_update']:checked").map(function () {
		bundleJarIdsFtp += (bundleJarIdsFtp != ""?",":"") + $(this).val();
    });
	
	//alert(bundleJarIds);
	updatePackageJarNameFtp = "";
	orginPackageJarNameFtp = "";
	
	var originJarName = $("#packageJarNameFtp").val().split(",");
	for(var i=0; i < originJarName.length; i++){
		var isFind = false;
		var matchOriStr = (originJarName[i] + "").match("-\\d\\.\\d");
		//alert(matchOriStr);
		if(matchOriStr != ""){
			var subJarName = originJarName[i].substring(0,originJarName[i].lastIndexOf(matchOriStr));
			//alert(subJarName);
			var updateJarName = packageJarNameFtp.split(",");
			for(var j=0; j < updateJarName.length; j++){
				var matchUpdateStr = (updateJarName[j] + "").match("-\\d\\.\\d");
				//alert(matchUpdateStr);
				if(matchUpdateStr != ""){
					var subUpdateJarName = updateJarName[j].substring(0,updateJarName[j].lastIndexOf(matchUpdateStr));
					if(subUpdateJarName == subJarName){
						//alert("find");
						updatePackageJarNameFtp += (updatePackageJarNameFtp != ""?",":"") + updateJarName[j];
						isFind = true;
						break;
					}else{
						isFind = false;
					}
				}
			}
			
			if(isFind == false){
				orginPackageJarNameFtp += (orginPackageJarNameFtp != ""?",":"") + originJarName[i];
			}
		
		}
	
	}
	var updateJarName = packageJarNameFtp.split(",");
	//alert(orginPackageJarName);
	for(var k=0; k < updateJarName.length; k++){
		//alert(updateJarName[k]);
		orginPackageJarNameFtp += (orginPackageJarNameFtp != ""?",":"") + updateJarName[k];
	}
	/*allPackageJarName = orginPackageJarName;
	alert(orginPackageJarName);*/
	$("#packageJarNameFtp").val(orginPackageJarNameFtp);
	//alert($("#packageJarName").val());
	$('#import_linkFtp').window('close'); 
}

/*function deleteFiles(fileName){
	$.post(BASE_URL_HTTP_INTERFACE + '/deleteFile',{"fileNames":fileNames},function(data){
		if(data.success){
			alert(data.msg);
		}else{
			alert(data.msg);
		}
	});
}*/

function deleteFile(fileNames){
	//alert($("tr[name='"+fileNames+"'] td:eq(1)").text());
	if($("tr[name='"+fileNames+"'] td:eq(1)").text() == "上传失败"){
		$("tr[name='"+fileNames+"']").remove();
	}else{
		//fileNames+=fileName+',';
		$.post(BASE_URL_HTTP_INTERFACE + '/deleteFile',{"fileNames":fileNames},function(data){
			if(data.success){
				alert(data.msg);
			}else{
				alert(data.msg);
			}
		});
		$("tr[name='"+fileNames+"']").remove();
		var packageName = [];
		var packageJarNameValue = "";
		//packageName.push(obj.fileName);
		if($("#packageJarNameFtp").val().indexOf(",") != -1){
			packageName = $("#packageJarNameFtp").val().split(","); 
			for(var i=0; i < packageName.length; i++){
				if(packageName[i] == fileNames){
					
				}else{
					packageJarNameValue += (packageJarNameValue != ""? ",":"") + packageName[i];
					//alert(packageJarName);
				}
			}
		}else{
			packageJarNameValue = "";
			$("#uploadTableFtp, #contentFtp").css("display", "none");
		}
		
		$("#packageJarNameFtp").val(packageJarNameValue);
	}
	
}
